<?php
namespace app\admin\controller;
use think\Controller;
use think\Request;
use think\Db;
use think\facade\Cache;
use think\facade\Session;
use app\admin\model\Users;
use app\admin\model\Menu;
use app\admin\model\AccessLog;


class Common extends Controller
{
    protected $admin = array();
    protected $sysconfig  = array(); //系统配置
    
    public function initialize()
    {
        parent::initialize();
        $admin = Session::get('admin');
        if($this->request->isAjax()){
            if(empty($admin)){
                die(outputError('登录超时'));
            }
        }else{
            if(empty($admin)){
                $this->redirect('/admin/login');
            }
        }
        //var_dump($admin);die;
        $id = $admin['id'];
        $obj = new Users();
        if($this->request->isAjax()){
            if(!$obj->_checkStatus($id)){
                die(outputError($obj->getError()));
            }
        }else{
            if(!$obj->_checkStatus($id)){
                $this->redirect('/admin/login');
            }
        }
        
        $this->admin = $admin;
        
        $this->accessLog();
        
        $menu_model = new Menu();
        $menus = $menu_model->_getAuthMenu($admin['role_id']);
        
        $controller = $this->request->controller();
        $action = $this->request->action();
        
        $sys = $this->getConfig();
        
        $this->sysconfig = $sys;
        $this->assign([
            'admin' => $admin,
            'left_menus' => $menus,
            'current_con' => $controller,
            'current_act' => strtolower($action),
            'sysconfig' => $sys,
        ]);
        
        
    }
    
    /**
    * 获取系统缓存信息
    */
    public function getConfig(){
        $config = Cache::get(config('sysconfig'));
        if(empty($config)){
            $this->setConfig();
            $config = Cache::get(config('sysconfig'));
        }
        $config = json_decode($config, true);
        $this->sysconfig = $config;
        return $config;
    }
    
    /**
    * 将信息写入缓存
    */
    public function setConfig(){
        $data = Db::name('set')->field('set_key as `key`, set_val as `val`')->select();
        $sys = [];
        foreach($data as $k=>$v){
            $sys[$v['key']] = $v['val'];
        }
        $value = json_encode($sys);
        
        Cache::set(config('sysconfig'),$value,3600);
    }

    public function hello($name = 'ThinkPHP5')
    {
        return 'hello,' . $name;
    }
    
    
    
    
    /**
    * 校验权限
    * @return bool
    */
    public function checkAuth(){
//        $admin_model = new \app\admin\model\Users();
//        $admin = $this->admin;
//        $admin = $admin_model->_findOne($admin['id']);
//        $role_model = new \app\admin\model\Role();
//        $role = $role_model->_findOne($admin->role_id);
//        if(!$role){
//            return false;
//        }
//
//        if(empty($role['auth_ids'])){
//            return false;
//        }
//
//        $auth_ids = empty($role['auth_ids']) ? [] : explode(',', $role['auth_ids']);
//
//        $controller = $this->request->controller();
//        $action = $this->request->action();
//
//        $controller = strtolower($controller);
//        $action = strtolower($action);
//
//        $auth_model = new \app\admin\model\Auth();
//        $auth_data = $auth_model->field('id,controller,action')->where([['controller', '=', $controller], ['action', '=', $action]])->find();
//        if(empty($auth_data)){
//            return false;
//        }
//
//
//
//        $auth_id = $auth_data['id'];
//
//        if(!in_array($auth_id, $auth_ids)){
//            return false;
//        }
        
        return true;
    }
    
    
    /**
    * 校验权限后操作
    */
    public function doCheckAuth($noCheckAction){
        $action = $this->request->action();
        foreach($noCheckAction as $k=>$v){
            $noCheckAction[$k] = strtolower($v);
        }

        if(empty($noCheckAction) || !in_array($action, $noCheckAction)){
            $isAuth = $this->checkAuth();
            if($this->request->isAjax()){
                if(!$isAuth){
                    die(outputError('无操作权限，请联系管理员'));
                }
            }else{
                if(!$isAuth){
                    die('无操作权限，请联系管理员');
                }
                
            }
        }
        
    }
    /**
    * 生成操作日志
    */
    public function accessLog(){
        $controller = $this->request->controller();
        $action = $this->request->action();
        
        $controller = strtolower($controller);
        $action = strtolower($action);
        if($controller == 'accesslog'){
            return true;
        }
        $auth_model = new \app\admin\model\Auth();
        $auth_data = $auth_model->field('id,controller,action')->where([['controller', '=', $controller], ['action', '=', $action]])->find();
        //var_dump($controller);die;
        
        $model = new AccessLog;
        $admin = $this->admin;
        $data['uid'] = $admin['id'];
        $data['aid'] = $auth_data['id'];
        $ret = $model->_create($data);
        if(!$ret){
            die(outputError('操作错误'));
        }
    }
    
    /**
    * 获取菜单
    */
    public function menus(){
        
    }
}
